package org.enhance.idempotent.demo.api.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.enhance.idempotent.demo.app.service.TestIdempotentAnnotationService;
import org.enhance.idempotent.demo.domain.entity.OrderEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 测试幂等注解
 *
 * @author wenpan 2023/01/07 10:13
 */
@Slf4j
@RestController("TestIdempotentAnnotationController.v1")
@RequestMapping("/v1/idempotent-annotation")
public class TestIdempotentAnnotationController {

    @Autowired
    private TestIdempotentAnnotationService testIdempotentAnnotationService;

    /**
     * 测试幂等注解
     * 请求URL：localhost:12345/v1/idempotent-annotation/add-order?orderNumber=TB-202301070001&orderAmount=1000&platformCode=taobao
     */
    @PostMapping("/add-order")
    public OrderEntity addOrder(@RequestParam String orderNumber,
                                @RequestParam String orderAmount,
                                @RequestParam String platformCode) {
        log.info("======>>>>>>addOrder orderNumber is {}, orderAmount is {}, platformCode is {}", orderNumber, orderAmount, platformCode);
        // 构建待新增的订单信息
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderNumber(orderNumber);
        orderEntity.setOrderAmount(new BigDecimal(orderAmount));
        orderEntity.setOrderStatus(1);
        orderEntity.setPlatformCode(platformCode);

        // 新增订单
        orderEntity = testIdempotentAnnotationService.addOrder(orderEntity);

        log.info("======>>>>>>addOrder result is {}", JSON.toJSONString(orderEntity));
        return orderEntity;
    }

    /**
     * 测试幂等注解
     * <p>
     * 请求URL：localhost:12345/v1/idempotent-annotation/modify-order
     * 入参：
     * {
     * "platformCode": "tianmao",
     * "orderNumber": "TM-202301070001",
     * "orderAmount": "1000",
     * "orderStatus": 1,
     * "orderLineEntityList": [
     * {
     * "orderLineNumber": "line-202301070001",
     * "skuCode": "skuCode202301070001",
     * "unitPrice": "200"
     * },
     * {
     * "orderLineNumber": "line-202301070002",
     * "skuCode": "skuCode202301070001",
     * "unitPrice": "200"
     * }
     * ]
     * }
     * </p>
     */
    @PostMapping("/modify-order")
    public OrderEntity modifyOrder(@RequestBody OrderEntity orderEntity) {
        log.info("=====>>>>>modifyOrder input params is : {}", JSON.toJSONString(orderEntity));
        // 修改
        orderEntity = testIdempotentAnnotationService.modifyOrder(orderEntity);
        log.info("=====>>>>>modifyOrder input result is : {}", JSON.toJSONString(orderEntity));
        return orderEntity;
    }

}
